from auther.models import AdminUser
from salarys.models import *
from utils.token import Token

inctyp = {
    '1': "综合所得",
    '2': "分类所得",
    '3': "非居民所得"
}
incite = {
    '101': "正常工资薪金",
    '102': "全年一次性奖金收入",
    '103': "稿酬所得",
    '104': "一般劳务报酬所得",
    '105': "解除劳动合同一次性补偿金",
    '106': "保险营销员佣金收入",
    '107': "证券经纪人佣金收入",
    '108': "其他连续劳务报酬所得",
    '109': "个人股权激励收入",
    '110': "年金领取",
    '111': "其他非连续劳务报酬",
    '201': "上市公司股息红利所得（沪市、深市、创业板）",
    '209': "其他利息、股息、红利",
    '210': "其他财产转让所得",
    '211': "其他偶然所得",
    '212': "其他财产租赁所得",
    '213': "股权转让所得",
    '301': "无住所个人正常工资薪金",
    '302': "无住所个人数月奖金",
    '303': "稿酬所得",
    '304': "一般劳务报酬所得",
    '305': "其他利息、股息、红利所得",
    '306': "股权转让所得",
    '307': "其他财产转让所得",
    '308': "偶然所得",
    '312': "非居民股权激励所得",
}


def get_salary(token, name, period, page, limit):
    return_data = {'code': 500, "msg": "无数据", 'data': [], 'pageCount': ''}
    token_class = Token()
    token_class.token = token
    user_id = token_class.check_token()
    if user_id:
        kwargs = {}
        args = ()
        if name:
            kwargs["search_result_stfnam__contains"] = name
        if period:
            kwargs["search_result_period"] = period
        # 根据token获取查询范围
        office_code = AdminUser.objects.filter(id=user_id).values('xft_account__xft_tax_office_code')
        if office_code.exists():
            kwargs["search_result_regseq"] = office_code[0]['xft_account__xft_tax_office_code']

        tax_results_list = TaxSearchResult.objects.filter(*args, **kwargs).values(
            'search_result_period',
            'search_result_stfnam',
            'search_result_natnlt',
            'search_result_certyp',
            'search_result_cernbr',
            'search_result_orgseq',
            'search_result_regseq',
            'search_result_durtax',
            'search_result_taxorg',
            'search_result_prjnam',
            'search_result_depnam',
            'search_result_depnbr',
            'search_result_inctyp',
            'search_result_isbmba',
            'search_result_nicnam',
            'search_result_incite',
            'search_result_groinc',
            'search_result_ybtsef',
            'search_result_jcfyfd',
            'search_result_crmssd',
            'search_result_jbylbx',
            'search_result_jbyibx',
            'search_result_syvxye',
            'search_result_zfgjjy',
            'search_result_annuty',
            'search_result_syjkbx',
            'search_result_syylbx',
            'search_result_qtkchj',
            'search_result_fudval',
            'search_result_othinf',
            'search_result_yykcsf',
            'search_result_zykcjk',
            'search_result_jmsezd',
            'search_result_ljykjn',
            'search_result_ljyjse',
            'search_result_ykjsef',
            'search_result_yjsefd',
            'search_result_ljsref',
            'search_result_ljjcfy',
            'search_result_ljzxkc',
            'search_result_ljyyef',
            'search_result_ljznjy',
            'search_result_ljsylr',
            'search_result_ljzflx',
            'search_result_ljzfzj',
            'search_result_ljjxjy',
            'search_result_lzxfjk',
            'search_result_ljqkce',
            'search_result_ljzykd',
            'search_result_ynssde',
            'search_result_sskcsf',
            'search_result_ynsefd',
            'search_result_taxrat',
            'search_result_remark',
            'search_result_ptsism',
            'search_result_sfctrt',
            'search_result_revcou',
            'search_result_orgcod',
            'search_result_whanam',
            'search_result_ybtses',
            'search_result_orgnam'
        )[(page - 1) * limit:limit * page]
        if tax_results_list.exists():
            return_data['code'] = 200
            return_data['msg'] = 'ok'
            return_data['pageCount'] = TaxSearchResult.objects.filter(*args, **kwargs).count()
            for index, tax in enumerate(tax_results_list):
                return_data['data'].append({
                    'index': (page - 1) * limit + index + 1,
                    'period': tax['search_result_period'],
                    'inctyp': inctyp.get(tax['search_result_inctyp']),
                    'incite': incite.get(tax['search_result_inctyp'] + tax['search_result_incite']),
                    'groinc': tax['search_result_groinc'],
                    'ybtsef': tax['search_result_ybtsef'],
                    'stfnam': tax['search_result_stfnam'],
                    'natnlt': tax['search_result_natnlt'],
                    'certyp': tax['search_result_certyp'],
                    'cernbr': tax['search_result_cernbr'],
                    'orgseq': tax['search_result_orgseq'],
                    'regseq': tax['search_result_regseq'],
                    'durtax': tax['search_result_durtax'],
                    'taxorg': tax['search_result_taxorg'],
                    'prjnam': tax['search_result_prjnam'],
                    'depnam': tax['search_result_depnam'],
                    'depnbr': tax['search_result_depnbr'],
                    'isbmba': tax['search_result_isbmba'],
                    'nicnam': tax['search_result_nicnam'],
                    'jcfyfd': tax['search_result_jcfyfd'],
                    'crmssd': tax['search_result_crmssd'],
                    'jbylbx': tax['search_result_jbylbx'],
                    'jbyibx': tax['search_result_jbyibx'],
                    'syvxye': tax['search_result_syvxye'],
                    'zfgjjy': tax['search_result_zfgjjy'],
                    'annuty': tax['search_result_annuty'],
                    'syjkbx': tax['search_result_syjkbx'],
                    'syylbx': tax['search_result_syylbx'],
                    'qtkchj': tax['search_result_qtkchj'],
                    'fudval': tax['search_result_fudval'],
                    'othinf': tax['search_result_othinf'],
                    'yykcsf': tax['search_result_yykcsf'],
                    'zykcjk': tax['search_result_zykcjk'],
                    'jmsezd': tax['search_result_jmsezd'],
                    'ljykjn': tax['search_result_ljykjn'],
                    'ljyjse': tax['search_result_ljyjse'],
                    'ykjsef': tax['search_result_ykjsef'],
                    'yjsefd': tax['search_result_yjsefd'],
                    'ljsref': tax['search_result_ljsref'],
                    'ljjcfy': tax['search_result_ljjcfy'],
                    'ljzxkc': tax['search_result_ljzxkc'],
                    'ljyyef': tax['search_result_ljyyef'],
                    'ljznjy': tax['search_result_ljznjy'],
                    'ljsylr': tax['search_result_ljsylr'],
                    'ljzflx': tax['search_result_ljzflx'],
                    'ljzfzj': tax['search_result_ljzfzj'],
                    'ljjxjy': tax['search_result_ljjxjy'],
                    'lzxfjk': tax['search_result_lzxfjk'],
                    'ljqkce': tax['search_result_ljqkce'],
                    'ljzykd': tax['search_result_ljzykd'],
                    'ynssde': tax['search_result_ynssde'],
                    'sskcsf': tax['search_result_sskcsf'],
                    'ynsefd': tax['search_result_ynsefd'],
                    'taxrat': tax['search_result_taxrat'],
                    'remark': tax['search_result_remark'],
                    'ptsism': tax['search_result_ptsism'],
                    'sfctrt': tax['search_result_sfctrt'],
                    'revcou': tax['search_result_revcou'],
                    'orgcod': tax['search_result_orgcod'],
                    'whanam': tax['search_result_whanam'],
                    'ybtses': tax['search_result_ybtses'],
                    'orgnam': tax['search_result_orgnam'],
                })
        return return_data
